﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Admx_Generator
{
    internal partial class FrmNewProject : Form
    {
        private List<AdmxProject> _openedProjects;

        internal FrmNewProject(List<AdmxProject> openedProjects)
        {
            InitializeComponent();
            this.SetupUI();
            this._openedProjects = openedProjects;
#if(DEBUG)
            this.flTxttBxProjectName.Text = "Admx Generator";
#endif
            this.flTxttBxProjectName.Select();
        }

        #region {properties}

        internal string WorkingFolder { get; private set; }

        internal string ProjectName { get; private set; }

        #endregion {properties}

        #region {private methods}

        /// <summary>
        /// Set UI accordingly to the application settings
        /// </summary>
        private void SetupUI()
        {
            List<char> allowedChars = Utilities.GetAllowedCharacters();

            // TextBox Folder
            try
            {
                string workingFolder = Utilities.GetExpandedPath(Properties.Settings.Default.WorkingFolder);

                this.txtBxFolder.Text = workingFolder;
                if (!System.IO.Directory.Exists(workingFolder))
                {
                    try { System.IO.Directory.CreateDirectory(workingFolder); }
                    catch (Exception) { }
                }
            }
            catch (Exception) { }

            // FilterTextBox Project Name
            try
            {
                this.flTxttBxProjectName.AllowedCharacters.AddRange(allowedChars);
                this.flTxttBxProjectName.AllowedCharacters.Add('_');
                this.flTxttBxProjectName.AllowedCharacters.Add('-');
                this.flTxttBxProjectName.AllowedCharacters.Add(' ');
            }
            catch (Exception) { }
        }

        /// <summary>
        /// Check user entries and allow or disallow the 'Ok' button accordingly
        /// </summary>
        private void ValidateData()
        {
            bool folderOK = !String.IsNullOrEmpty(this.txtBxFolder.Text) && System.IO.Directory.Exists(Utilities.GetExpandedPath(this.txtBxFolder.Text));
            bool projectNameOK = !String.IsNullOrEmpty(this.flTxttBxProjectName.Text) && !System.IO.Directory.Exists(System.IO.Path.Combine(Utilities.GetExpandedPath(this.txtBxFolder.Text), this.flTxttBxProjectName.Text));
            bool alreadyOpenedProject = false;

            if (!String.IsNullOrEmpty(this.flTxttBxProjectName.Text))
            {
                foreach (AdmxProject openedProject in this._openedProjects)
                {
                    if (openedProject.ProjectName.ToLower() == this.flTxttBxProjectName.Text.ToLower())
                    {
                        alreadyOpenedProject = true;
                        break;
                    }
                }
            }

            // Displays UI clues
            this.txtBxFolder.BackColor = folderOK ? SystemColors.Window : Color.Brown;
            this.flTxttBxProjectName.BackColor = (projectNameOK && !alreadyOpenedProject) ? SystemColors.Window : Color.DarkOrange;

            this.btnOk.Enabled = folderOK && projectNameOK;
        }

        #endregion {private methods}

        #region {UI Management}

        private void control_TextChanged(object sender, EventArgs e)
        {
            this.ValidateData();
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fldBrowser = new FolderBrowserDialog();

            if (System.IO.Directory.Exists(Utilities.GetExpandedPath(this.txtBxFolder.Text)))
            { fldBrowser.SelectedPath = Utilities.GetExpandedPath(this.txtBxFolder.Text); }
            else if (System.IO.Directory.Exists(Utilities.GetExpandedPath(Properties.Settings.Default.WorkingFolder)))
            { fldBrowser.SelectedPath = Utilities.GetExpandedPath(Properties.Settings.Default.WorkingFolder); }

            fldBrowser.ShowNewFolderButton = true;
            if (fldBrowser.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            { this.txtBxFolder.Text = fldBrowser.SelectedPath; }
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            this.WorkingFolder = this.txtBxFolder.Text;
            this.ProjectName = this.flTxttBxProjectName.Text;

            DialogResult = System.Windows.Forms.DialogResult.OK;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            DialogResult = System.Windows.Forms.DialogResult.Cancel;
        }

        private void txtControl_Enter(object sender, EventArgs e)
        {
            TextBox txtControl = (TextBox)sender;

            txtControl.Select(txtControl.Text.Length, 0);
        }

        #endregion {UI Management}
    }
}
